#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
#define sort stable_sort
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template<typename TP> inline TP read(TP &num)
{
	TP x=0;
	int f=0;
	char ch=getchar();
	while(ch<48||ch>57) f|=ch=='-',ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return num=f?-x:x;
}
template<typename ...Args> inline void read(Args &...args)
{
	(read(args),...);
}
template<typename TP> inline void write(TP x)
{
	(x<0)?(putchar('-'),x=-x):0;
	(x>9)?(write(x/10),0):0;
	putchar((x%10)^48);
}
template<typename TP> inline void writeln(TP x)
{
	write<TP>(x);
	puts("");
}
signed main()
{
	freopen("memory.in","r",stdin);
	freopen("memory.out","w",stdout);
    int n,q; read(n,q);
    vector<vector<pair<int,int>>> g(n+1);
    rep(i,1,n-1)
    {
        int u,v,w; read(u,v,w);
        g[u].emplace_back(v,w),g[v].emplace_back(u,w);
    }
    vector<int> fa(n+1),dep=fa,siz=fa,son=fa,top=fa,dis=fa;
    function<void(int,int)> dfs=[&](int x,int fat)->void
    {
        fa[x]=fat,dep[x]=dep[fat]+1,siz[x]=1;
        for(auto [y,w]:g[x]) if(y!=fat)
        {
            dis[y]=dis[x]+w,dfs(y,x),siz[x]+=siz[y];
            if(siz[y]>siz[son[x]]) son[x]=y;
        }
    }; dfs(1,0);
    function<void(int,int)> decomp=[&](int x,int topf)->void
    {
        top[x]=topf;
        if(son[x]) decomp(son[x],topf);
        for(auto [y,w]:g[x]) if(y!=fa[x]&&y!=son[x]) decomp(y,y);
    }; decomp(1,1);
    auto lca=[&](int x,int y)->int
    {
        while(top[x]!=top[y])
        {
            if(dep[top[x]]<dep[top[y]]) swap(x,y);
            x=fa[top[x]];
        }
        return dep[x]<dep[y]?x:y;
    };
    vector<vector<int>> st(21,vector<int>(n+1,0));
    auto build_st=[&]()->void
    {
        rep(i,1,n) st[0][i]=i;
        rep(k,1,20) rep(i,1,n)
        {
            if(i+(1<<k)-1>n) break;
            st[k][i]=lca(st[k-1][i],st[k-1][i+(1<<(k-1))]);
        }
    }; build_st();
    auto query=[&](int l,int r)->int
    {
        int k=__lg(r-l+1);
        return lca(st[k][l],st[k][r-(1<<k)+1]);
    };
    int lst=0,avail=1;
    /*
     * the p=1 property is hackable
     * since p is encoded, and we know p=1, therefore, next query's p xor 1 is current query's answer
     * and only the last query needs to be calculated
     * example:
        rep(i,1,q)
        {
            int p,l,r; read(p,l,r);
            if(i>1) writeln(p^1);
            if(i==q) l^=p^1,r^=p^1,writeln(dis[query(l,r)]);
        }
     */
    while(q--)
    {
        int p,l,r; read(p,l,r);
        p^=lst,l^=lst,r^=lst;
        if(p==1&&avail) writeln(lst=dis[query(l,r)]);
        else
        {
            son.assign(n+1,0),dep.assign(n+1,0),dis.assign(n+1,0);
            dfs(p,0),decomp(p,p),build_st(),avail=0;
            writeln(lst=dis[query(l,r)]);
        }
    }
	return 0;
}